package com.sintatico;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "sintatico")
@SessionScoped
public class SintaticoBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private String sentenca;
	private String valida;
	private String iteracao;
	private String sentencaGerada;

	private List<String> actions;
	private List<String> actionsEntrada;
	private List<String> actionsPilha;

	private SintaticoModel sintaticoModel = new SintaticoModel();

	public String getValida() {
		return valida;
	}

	public void setValida(String valida) {
		this.valida = valida;
	}

	public String getIteracao() {
		return iteracao;
	}

	public void setIteracao(String iteracao) {
		this.iteracao = iteracao;
	}

	public String getSentenca() {
		return sentenca;
	}

	public void setSentenca(String sentenca) {
		this.sentenca = sentenca;
	}

	public SintaticoModel getSintaticoModel() {
		return sintaticoModel;
	}

	public List<String> getActions() {
		if (actions == null) {
			actions = new ArrayList<String>();
		}
		return actions;
	}

	public void setActions(List<String> actions) {
		this.actions = actions;
	}

	public List<String> getActionsEntrada() {
		if (actionsEntrada == null) {
			actionsEntrada = new ArrayList<String>();
		}
		return actionsEntrada;
	}

	public void setActionsEntrada(List<String> actionsEntrada) {
		this.actionsEntrada = actionsEntrada;
	}

	public List<String> getActionsPilha() {
		if (actionsPilha == null) {
			actionsPilha = new ArrayList<String>();
		}
		return actionsPilha;
	}

	public void setActionsPilha(List<String> actionsPilha) {
		this.actionsPilha = actionsPilha;
	}

	public String getSentencaGerada() {
		return sentencaGerada;
	}

	public void setSentencaGerada(String sentencaGerada) {
		this.sentencaGerada = sentencaGerada;
	}

	public void verificaSentenca() {
		String sentenca = getSentenca().replaceAll("\\W", "");

		getActions().clear();
		getActionsEntrada().clear();
		getActionsPilha().clear();

		if (sentenca.length() > 0) {
			boolean valida = getSintaticoModel().analisaSentenca(sentenca);

			if (valida) {
				FacesContext.getCurrentInstance().addMessage("success", new FacesMessage("Sentenca Correta!"));
			} else {
				FacesContext.getCurrentInstance().addMessage("error", new FacesMessage("Sentenca Incorreta!"));
			}

			setActions(getSintaticoModel().getActions());
			setActionsEntrada(getSintaticoModel().getActionsEntrada());
			setActionsPilha(getSintaticoModel().getActionsPilha());

			setIteracao(getSintaticoModel().getIteracao() + "");
		}
	}

	public void geraSentenca() {
		GeradorModel geradorModel = new GeradorModel();
		setSentencaGerada(geradorModel.geraSentenca());
	}

}
